//#define BLYNK_DEBUG
#define BLYNK_PRINT stdout
#ifdef RASPBERRY
  #include <BlynkApiWiringPi.h>
#else
  #include <BlynkApiLinux.h>
#endif
#include <BlynkSocket.h>
#include <BlynkOptionsParser.h>

static BlynkTransportSocket _blynkTransport;
BlynkSocket Blynk(_blynkTransport);
#include <BlynkWidgets.h>

static const char *auth, *serv;
static uint16_t port;

int setTemp1 = 30,setTemp2 = 25;
long HX711_Buffer = 0;
long Weight_Maopi = 0,Weight_Shiwu = 0;
float Weight = 0;
FILE *stream1, *stream2, *stream3, *stream4;
#include <sys/types.h>
#include <sys/stat.h> 
#include <sys/sysinfo.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
#include <wiringPi.h>
#include <pthread.h>
#include <vector>
#include <time.h>
#include <string>
#include <sstream>
using namespace std;

#define BUFSIZE 128
#define pinPWM 13
#define HX711_SCK 27  //这根是绿线
#define HX711_DT 17	//需要遵循BCM编码方式，如果在wiringPI目录下则另外遵循wifingpi
#define GapValue 445

unsigned long HX711_Read(void)  //增益128
{
  unsigned long count; 
  unsigned char i;
  bool Flag = 0;

  digitalWrite(HX711_DT, HIGH);
  delayMicroseconds(1);

  digitalWrite(HX711_SCK, LOW);
  delayMicroseconds(1);

    count=0; 
    while(digitalRead(HX711_DT)); 
    for(i=0;i<24;i++)
  { 
      digitalWrite(HX711_SCK, HIGH); 
    delayMicroseconds(1);
      count=count<<1; 
    digitalWrite(HX711_SCK, LOW); 
    delayMicroseconds(1);
      if(digitalRead(HX711_DT))
      count++; 
  } 
  digitalWrite(HX711_SCK, HIGH); 
  count ^= 0x800000;
  delayMicroseconds(1);
  digitalWrite(HX711_SCK, LOW); 
  delayMicroseconds(1);
  
  return(count);
}
void Init_Hx711()
{
  pinMode(HX711_SCK, OUTPUT); 
  pinMode(HX711_DT, INPUT);
}
void Get_Maopi()
{
  Weight_Maopi = HX711_Read();    
} 
long Get_Weight()
{
  HX711_Buffer = HX711_Read();
  Weight_Shiwu = HX711_Buffer;
  Weight_Shiwu = Weight_Shiwu - Weight_Maopi;       //
  Weight_Shiwu = (long)((float)Weight_Shiwu/GapValue);  
  return Weight_Shiwu;
}
BLYNK_WRITE(V2)
{
	setTemp1 = param.asInt();
    //printf("Got a value: %d\n", param.asInt());
}
BLYNK_WRITE(V3)
{
	setTemp2 = param.asInt();	
	//printf("Got a value: %s\n", param[0].asStr());
}

BLYNK_WRITE(V47)
{
	if(param.asInt())
	{
		stream2 = popen("sudo shutdown -h now", "w");
	}	
	else
	{
		pclose(stream2);
	}
}

void readTemp()
{
	float temp;
	int i, j;
    int fd;
	int ret;
	int keepfan;
	char buf[BUFSIZE];
	char tempBuf[5];
	fd = open("/sys/bus/w1/devices/28-0416c43c57ff/w1_slave", O_RDONLY);
	ret = read(fd, buf, BUFSIZE);
	for(i=0;i<sizeof(buf);i++){
		if(buf[i] == 't'){
			for(j=0;j<sizeof(tempBuf);j++)
				tempBuf[j] = buf[i+2+j]; 				
			}	
		}
	temp = (float)atoi(tempBuf) / 1000;
	if(temp > setTemp1) {
		digitalWrite(5, HIGH);
		digitalWrite(6, LOW);
		keepfan = 1;
	}
	else {
		if(keepfan == 1)
		{
			digitalWrite(5, HIGH);
			digitalWrite(6, LOW);
		}
	}
	if(temp < setTemp2) {
		digitalWrite(5, LOW);
		digitalWrite(6, LOW);
		keepfan = 0;
	}
	Blynk.virtualWrite(V1, temp);
	//printf("%.3f C\n",temp);
	//sleep(0.25);		
	close(fd);	
}
void readDepth()
{
	int keeppump = 0;
	if(digitalRead(23) == 0)
	{
		Blynk.virtualWrite(V4, 0);
		digitalWrite(7, HIGH);
		digitalWrite(8, LOW);
		keeppump = 1;
	}
	else 
	{
		Blynk.virtualWrite(V4, 1);
		if(keeppump == 1)
		{
			digitalWrite(7, HIGH);
			digitalWrite(8, LOW);
		}
	}
	if(digitalRead(24) == 1)
	{
		Blynk.virtualWrite(V4, 2);
		digitalWrite(7, LOW);
		digitalWrite(8, LOW);
		keeppump = 0;
	}
}
void readWeight()
{
	Weight = Get_Weight();	//计算放在传感器上的重物重量
	// printf("%f", float(Weight));	//串口显示重量
	// printf(" g\n");	//显示单位
	// printf("\n");		//显示单位
	// delay(1000);				//延时1s
	Blynk.virtualWrite(V30, Weight);
}

PI_THREAD(readAll)
{
	Init_Hx711();				//初始化HX711模块连接的IO设置
	delay(1000);
	Get_Maopi();		//获取毛皮
	while(true)
	{
		readTemp();
		readDepth();	
		if(digitalRead(16) == 0)
		{
			printf("meet\n");
			readWeight();
		}
	}
}

void setup()
{
	//int startTime = mills();	//设定开始的时间
	wiringPiSetup () ;	
	piThreadCreate(readAll);	//创建线程，只能额外创建一个线程
	pinMode(5, OUTPUT);
	pinMode(6, OUTPUT);
	pinMode(7, OUTPUT);
	pinMode(8, OUTPUT);
	pinMode(20, OUTPUT);
	pinMode(21, OUTPUT);
	pinMode(26, OUTPUT);
	pinMode(23, INPUT);
	pinMode(24, INPUT);
	pinMode(16, INPUT);
	//pinMode(pinPWM, PWM_OUTPUT);
    Blynk.begin(auth, serv, port);
}

void loop()
{
    Blynk.run();
}


int main(int argc, char* argv[])
{
    parse_options(argc, argv, auth, serv, port);

    setup();
    while(true) {
        loop();
    }

    return 0;
}
